package com.fs.cv.service.impl;

import com.fs.cv.dao.CourseInfoDao;
import com.fs.cv.pojo.pojodao.Course;
import com.fs.cv.pojo.pojodao.User;
import com.fs.cv.pojo.pojoview.CourseInfo;
import com.fs.cv.service.CourseService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CourseServiceImpl implements CourseService {
    @Autowired
    private CourseInfoDao courseInfoDao;

    //查询课程课程的所有数据
    @Override
    public List<Course> queryAll() {
        return courseInfoDao.queryAll();
    }

    //查询课程表和专业表
    @Override
    public List<CourseInfo> queryAllCourseDepartment() {
        //获取当前的登录对象
        Session session = SecurityUtils.getSubject().getSession();
        User user = (User) session.getAttribute("user");
        int userId=user.getUserId();

        return courseInfoDao.queryAllCourseDepartment(userId);
    }

    //添加课程表和专业表
    @Override
    public boolean addCourseDepartment(CourseInfo courseInfo) {
        int result = courseInfoDao.addCourseDepartment(courseInfo);
        return result > 0;
    }

    //删除一门课程表
    @Override
    public int deleteOneCourse(Integer courseId) {
        System.out.println(courseId);
        return courseInfoDao.deleteOneCourse(courseId);
    }
    //编辑课程查询的当前课表信息
    @Override
    public CourseInfo currentOneCourse(Integer courseId) {
        System.out.println(courseId);
        return courseInfoDao.currentOneCourse(courseId);
    }

    @Override
    public Integer editOneCourse(CourseInfo courseInfo) {
        return courseInfoDao.editOneCourse(courseInfo);
    }

    @Override
    public PageInfo<CourseInfo> pageCourse(Integer currentPage, Integer pageSize) {
        //获取当前的登录对象
        Session session = SecurityUtils.getSubject().getSession();
        User user = (User) session.getAttribute("user");
        int userId=user.getUserId();

        //准备拦截mybatis后续的查询
        PageHelper.startPage(currentPage,pageSize);
        List<CourseInfo> courseInfos = courseInfoDao.queryAllCourseDepartment(userId);
        //pagehelper内部计算总共有多少页
        PageInfo<CourseInfo> pageInfo = new PageInfo<>(courseInfos);
        return pageInfo;
    }
}
